package com.jxpanda.spring.launcher.controller;

import com.jxpanda.spring.module.auth.endpoint.annotation.JwtClaimsUserId;
import com.jxpanda.spring.module.auth.endpoint.annotation.JwtClaimsScope;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/test")
public class TestController {


    @GetMapping("/test")
    @PreAuthorize("hasAnyAuthority('SCOPE_ROOT') or hasAnyAuthority('SCOPE_ADMIN')")
    public Mono<Object> test(@JwtClaimsUserId String userId, @JwtClaimsScope List<String> scopes) {
        return Mono.just("Hello:" + userId + ",scope:" + String.join(",", scopes));
    }

    @Getter
    @Setter
    public static class Test {
        private String name;
        private Integer age;
        private List<String> roles;
        private Long aaLong;
        private Float aaFloat;
        private Double aaDouble;
        private BigDecimal bigDecimal;
        private Boolean aaBoolean;
        private LocalDateTime localDateTime = LocalDateTime.now();
    }


    @GetMapping("/hello")
    @Secured({"ADMIN"})
    public Mono<Object> hello(@AuthenticationPrincipal Jwt jwt) {
        return Mono.just(jwt);
    }

    @GetMapping("/user")
    public Mono<String> getUserInfo() {
        return ReactiveSecurityContextHolder.getContext()
                .map(securityContext -> {
                    Authentication authentication = securityContext.getAuthentication();
                    String username = authentication.getName();
                    // 您可以从 Authentication 对象中获取更多信息
                    return "Hello, " + username;
                });
    }

}
